﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Medical.Business;
using Medical.Entity;

/// <summary>
/// 文章列表页
/// </summary>
public partial class ArticleList : AdminPage
{
    /// <summary>
    /// 文章类型
    /// </summary>
    protected ArticelTypeEnum eType;

    /// <summary>
    /// 分类级别
    /// </summary>
    protected int nSortCount = 0;

    /// <summary>
    /// 页面初始化
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        eType = (ArticelTypeEnum)Convert.ToInt32(Common.GetQueryValue("type", 0));
        if (!Page.IsPostBack)
        {
            InitSort();
            InitData();
        }
    }
    /// <summary>
    /// 页面数据初始化
    /// </summary>
    protected void InitData()
    {
        //分类级别
        switch (eType)
        {
            case ArticelTypeEnum.Disease:
                nSortCount = 2;
                break;
            case ArticelTypeEnum.Subject:
            case ArticelTypeEnum.Technology:
            case ArticelTypeEnum.Equipment:
                nSortCount = 0;
                break;
            case ArticelTypeEnum.News:
            case ArticelTypeEnum.Health:
                nSortCount = 1;
                break;
            case ArticelTypeEnum.Expert:
                ltlTitle.Text = "姓名";
                ltlTitle1.Text = "姓名";
                break;
        }
        string strName = txtName.Text.Trim();
        int nRecordCount = 0;
        int nPageCount = 0;
        //文章信息列表绑定
        DataTable dt = null;
        BisArticle bisArticle = new BisArticle();
        //if (eType == ArticelTypeEnum.Technology)
        //{
        //    int nPartTypeID = Convert.ToInt32(ddlPartType.SelectedValue);
        //    dt = bisArticle.GetArticleListByPartType(nPartTypeID, nAdminFeatures == 1 ? Convert.ToInt32(ddlAdmin.SelectedValue) : nAdminUserID, eType, strName, txtBeginTime.Text.Trim(), txtEndTime.Text.Trim(), anpPage.CurrentPageIndex, anpPage.PageSize, ref nPageCount, ref nRecordCount);
        //}
        //else
        //{
        int nSortID = Convert.ToInt32(ddlSort.SelectedValue);
        dt = bisArticle.GetArticleList(nSortID, nAdminFeatures == 1 ? Convert.ToInt32(ddlAdmin.SelectedValue) : nAdminUserID, eType, strName, txtBeginTime.Text.Trim(), txtEndTime.Text.Trim(), 0, anpPage.CurrentPageIndex, anpPage.PageSize, ref nPageCount, ref nRecordCount);
        //}
        anpPage.RecordCount = nRecordCount;
        if (nRecordCount == 0)
        {
            noData.Style["display"] = "block";
        }
        else
        {
            noData.Style["display"] = "none";
        }
        rptNewsInfo.DataSource = dt;
        rptNewsInfo.DataBind();
        dt.Dispose();


    }

    protected void InitPartType()
    {
        ddlPartType.Items.Add(new ListItem("所有分类", "0"));
        DataTable dtPartType = new BisSortInfo().GetAllSortList(ArticelTypeEnum.Disease);
        DataRow[] dr = dtPartType.Select("ParentID=0");
        foreach (DataRow temdr in dr)
        {
            ddlPartType.Items.Add(new ListItem(temdr["Name"].ToString(), temdr["ID"].ToString()));
        }
    }

    /// <summary>
    /// 初始化分类下拉框
    /// </summary>
    protected void InitSort()
    {
        BisSortInfo bisSort = new BisSortInfo();
        ddlSort.Items.Add(new ListItem("所有分类", "0"));
        //绑定分类下拉框
        DataTable dtSortList = bisSort.GetAllSortList((ArticelTypeEnum)eType);
        if (dtSortList != null && dtSortList.Rows.Count > 0)
        {
            int nSortIndex = 0;
            foreach (DataRow dr in dtSortList.Rows)
            {
                nSortIndex = Convert.ToInt32(dr["SortIndex"]);
                if (nSortIndex == 1 && (eType == ArticelTypeEnum.News || eType == ArticelTypeEnum.Health))
                    continue;
                ddlSort.Items.Add(new ListItem(Common.GetSortIcon(nSortIndex) + dr["Name"].ToString(), dr["ID"].ToString()));
            }
            dtSortList.Dispose();
        }
        BisAdminInfo bisAdminInfo = new BisAdminInfo();
        ddlAdmin.Items.Add(new ListItem("全部", "0"));
        DataTable dtAdmin = bisAdminInfo.GetAdminList();
        foreach (DataRow drAdmin in dtAdmin.Rows)
        {
            ddlAdmin.Items.Add(new ListItem(drAdmin["UserName"].ToString(), drAdmin["ID"].ToString()));
        }
        if (!SupportFeatures(1))
        {
            ltlMan.Visible = false;
            ddlAdmin.Visible = false;
        }
    }


    /// <summary>
    /// 列表绑定处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rptNewsInfo_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DataTable dtArticle = (DataTable)((Repeater)sender).DataSource;
        ImageButton btnDelete = e.Item.FindControl("btnDelete") as ImageButton;
        if (dtArticle.Rows[e.Item.ItemIndex]["PartType"] != DBNull.Value && dtArticle.Rows[e.Item.ItemIndex]["PartType"].ToString() == "1")
        {
            btnDelete.Enabled = false;
            btnDelete.ImageUrl = "images/nodelete.gif";
        }
    }

    /// <summary>
    /// 列表事件
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    protected void rptNewsInfo_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            //删除信息
            string[] itemList = e.CommandArgument.ToString().Split(',');
            bool result = new BisArticle().Delete(Convert.ToInt32(itemList[0]));
            if (result)
            {
                if (itemList.Length > 1 && !string.IsNullOrEmpty(itemList[1]))
                    Until.DeleteFile(Server.MapPath("~/" + WebConfigInfo.FilePlace + "d/" + itemList[1]));
                InitData();
            }
            Until.Alert(result ? "删除成功！" : "删除失败！");
        }
        else if (e.CommandName == "Hot")
        {
            //设置热门状态
            string[] itemList = e.CommandArgument.ToString().Split(',');
            bool result = new BisArticle().SetHot(Convert.ToInt32(itemList[0]), !Convert.ToBoolean(itemList[1]));
            InitData();
        }
        else if (e.CommandName == "Top")
        {
            //设置推荐状态
            string[] itemList = e.CommandArgument.ToString().Split(',');
            bool result = new BisArticle().SetTop(Convert.ToInt32(itemList[0]), !Convert.ToBoolean(itemList[1]));
            InitData();
        }
        else if (e.CommandName == "Order")
        {
            TextBox txtOrder = e.Item.FindControl("txtOrder") as TextBox;
            int nOrder = 0;
            Int32.TryParse(txtOrder.Text.Trim(), out nOrder);
            if (nOrder > 0)
            {
                new BisArticle().SetOrder(Convert.ToInt32(e.CommandArgument), nOrder);
            }
            InitData();
        }
    }

    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="src"></param>
    /// <param name="e"></param>
    protected void anpPage_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        anpPage.CurrentPageIndex = e.NewPageIndex;
        InitData();
    }

    /// <summary>
    /// 查询
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        anpPage.CurrentPageIndex = 1;
        InitData();
    }

    /// <summary>
    /// 重置
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnReset_Click(object sender, EventArgs e)
    {
        anpPage.CurrentPageIndex = 1;
        txtName.Text = "";
        ddlSort.SelectedValue = "0";
        ddlAdmin.SelectedValue = "0";
        txtBeginTime.Text = string.Empty;
        txtEndTime.Text = string.Empty;
        InitData();
    }

    protected string CheckDisplay()
    {
        if (eType == ArticelTypeEnum.About)
            return "display:none";
        return "";
    }

    protected string CheckDisplay1()
    {
        if (eType != ArticelTypeEnum.Disease)
            return "display:none";
        return "";
    }

    protected string CheckCss()
    {
        if (eType == ArticelTypeEnum.About)
            return "";
        return "display:none";
    }

    protected string CheckCss1()
    {
        //if (eType == ArticelTypeEnum.Technology)
        //    return "";
        return "display:none";
    }

    protected string CheckCss2()
    {
        if (eType == ArticelTypeEnum.About 
            || eType == ArticelTypeEnum.Case 
            || eType == ArticelTypeEnum.Disease 
            || eType == ArticelTypeEnum.Expert
            || eType == ArticelTypeEnum.Health
            || eType == ArticelTypeEnum.News
            || eType == ArticelTypeEnum.Technology)
            return "";
        return "display:none";
    }

    protected string CheckCss3()
    {
        if (eType == ArticelTypeEnum.Equipment
            || eType == ArticelTypeEnum.Subject)
            return "";
        return "display:none";
    }
}
